define(['require' , 'exports' , 'module' , 'jquery' , 'layer' , 'common/common' , 'baiduTemplate'] , function (require , exports , module , $ , layer , common) {
    var getCode = function () {
        $('#get-code').bind('click' , function () {
            var __this = $(this);

            var fromData = {};
            fromData['mobile'] = $('#mobile').val();

            common.ajaxPost('/send/check/code' , fromData , function (error , data) {
                if(error === null && data.code == 0) {
                    __this.attr("disabled","disabled");
                    var time = 60;
                    var set=setInterval(function(){
                        __this.val(--time+"(s)");
                    }, 1000);
                    setTimeout(function(){
                        __this.attr("disabled",false).val("重新获取");
                        clearInterval(set);
                    }, 60000);
                }else if(error == null) {
                    layer.open({
                        content : data.msg,
                        skin : 'msg' ,
                        time : 2
                    });
                }else{
                    layer.open({
                        content : '发送短信失败' ,
                        skin : 'msg' ,
                        time : 2
                    });
                }
            });
        });
    };
    
    var bindMobile = function () {
        $('#submit').bind('click' , function () {
            var fromData = {};
            fromData['mobile']  = $('#mobile').val();
            fromData['code']    = $('#code').val();

            common.ajaxPost('/user/bind/mobile' , fromData , function (error , data) {
                if(error === null && data.code == 0) {
                    layer.open({
                        content : "绑定成功" ,
                        skin : 'msg' ,
                        time : 2
                    });
                    window.history.back();
                }else if(error == null) {
                    layer.open({
                        content : data.msg ,
                        skin : 'msg' ,
                        time : 2
                    });
                }else{
                    layer.open({
                        content : '发送短信失败' ,
                        skin : 'msg' ,
                        time : 2
                    });
                }
            });
        });  
    };

    var checkIsBindMobile = function () {
        $.ajax({
            url : '/user/check/mobile',
            type : 'GET',
            dataType : 'JSON',
            success : function (data) {
                console.log(data.data);
                if(data.code == 0) {
                    if (data.data != 1) {
                        window.location.href = '/user/bind/mobile';
                    }
                }
            },
            error : function (error) {
                console.log(error);
            }
        });
    };

    exports.checkIsBindMobile = checkIsBindMobile;
    exports.bindMobile = bindMobile;
    exports.getCode = getCode;
});
